import xml.etree.cElementTree as XmlET

tree = XmlET.parse("DataLog_config.xml")
root = tree.getroot()
'''
for child in root:
    #print(child.tag)#Database
    #print(child.attrib)#{'type': 'mysql', 'simulate': 'true'}
    #print(child.tag,child.attrib)
    #print(child.text)#empty
    for item in child:
        print(item.tag,item.text)
        
'''       
# 修改   将 localhost  的用户改为 fuml

for child in root:
    host = child.find('ServerHost').text
    if host == 'localhost' :
        child.find('UserName').text='fuml'
'''
for item in child:
    if item.text == 'nmc':
        item.text = 'fuml'
        break
'''
    
# 删除
for child in root:
    host = child.find('ServerHost').text
    if host == 'localhost' :
        #child.find('UserName').text='fuml'
        root.remove(child)
#tree.write("DataLog_config_mod.xml", encoding='UTF-8')        
# 生成xml文件

new_xml = XmlET.Element("namelist")

name = XmlET.SubElement(new_xml,"name1",attrib={"enrolled":"yes"})
age = XmlET.SubElement(name,"age",attrib={"checked":"no"})
age.text = "18"
gender = XmlET.SubElement(name,"gender")
gender.text ="male"

#create file
etree = XmlET.ElementTree(new_xml)
etree.write("namelist.xml", encoding='UTF-8',xml_declaration=True)

#send to stdout
XmlET.dump(new_xml)
